package cn.ygh.bbo.common.utils;

import groovy.lang.GroovyShell;
import groovy.lang.Script;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ygh
 * @Desc groovy脚本处理工具
 * @date 2019/10/1
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {


    private static final Pattern PATTERN=Pattern.compile("([A-Za-z\\d]+)(_)?");

    private static GroovyShell shell = null;

    /**
     * 单例shell
     * @return
     */
    public static GroovyShell getShell() {
        if (shell == null) {
            synchronized (StringUtils.class) {
                if (shell == null) {
                    shell = new GroovyShell();
                }
            }
        }
        return shell;
    }

    /**
     * 表列表查询字段添加  通过脚本动态添加 搜索字段
     * 2.user:"userId="+sessonobj+".id"
     *
     * @param value
     * @param map
     * @return
     */
    public static String getGroovyResult(String value, Map<String, Object> map) {
        if (org.apache.commons.lang3.StringUtils.isBlank(value)){
            return "";
        }
        GroovyShell shell = getShell();
        //添加参数
        if (map != null) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                shell.setVariable(entry.getKey(), entry.getValue());
            }
        }
        String str = "return " + value;
        Script script = shell.parse(str); //return "id=3"
        String ret = "" + script.run();
        ret = (ret.equals("null")) ? "" : ret;
        return ret;
    }

    /**
     * 驼峰法转下划线
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public static String camel2Underline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuilder sb = new StringBuilder();
        Matcher matcher = PATTERN.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰法
     *
     * @param line       源字符串
     * @param smallCamel 大小驼峰,是否为小驼峰
     * @return 转换后的字符串
     */
    public static String underline2Camel(String line, boolean smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Matcher matcher = PATTERN.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

}
